﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, Riccardo Marzi, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using Ionic.Utils.Zip;

namespace Fireball.Extensions
{
    public static class FileExtensions
    {
        /// <summary>
        /// Delete all files specified on the enumerable
        /// </summary>
        /// <param name="files">The files beings deleted</param>
        public static void DeleteAll(this IEnumerable<FileInfo> files)
        {
            foreach (FileInfo c in files)
                c.Delete();
        }

        /// <summary>
        /// Copy all files to the destination Path
        /// </summary>
        /// <param name="files">List of files to copy</param>
        /// <param name="destPath">The destination path where copy all files</param>
        /// <param name="overWrite">True for overwrite a existing file</param>
        public static void CopyTo(this IEnumerable<FileInfo> files, DirectoryInfo destDir, bool overWrite)
        {
            foreach (FileInfo c in files)
                c.CopyTo(Path.Combine(destDir.FullName, c.Name + c.Extension));
        }

        /// <summary>
        /// Copy all files to the destination Path
        /// </summary>
        /// <param name="files">List of files to copy, if a file exist it will not be overwriten</param>
        /// <param name="destDir">The destination path where copy all files</param>
        public static void CopyTo(this IEnumerable<FileInfo> files, DirectoryInfo destDir)
        {
            CopyTo(files, destDir, false);
        }

        /// <summary>
        /// Compress all Files on the destination zip file
        /// </summary>
        /// <param name="files">Files to add on the archive</param>
        /// <param name="destZipFile">Destination Zip File</param>
        public static void ZipFiles(this IEnumerable<FileInfo> files, string destZipFile)
        {
            using (ZipFile zipFile = new ZipFile(destZipFile))
            {
                foreach (FileInfo f in files)
                    zipFile.AddFile(f.FullName);

                zipFile.Save();
            }
        }
    }
}
